package libaryinterface.user;

import libaryinterface.interfaceutils.MyDialogDemo;
import pojo.vo.BookVo;
import service.BookRecordService;
import service.BookService;
import service.impl.BookRecordServiceImpl;
import service.impl.BookServiceImpl;

import java.awt.event.*;
import java.sql.SQLException;
import java.util.List;
import javax.swing.*;

//点选借书时长的界面
public class UserLendtimeInterface extends JFrame {
    JFrame f1;
    JPanel p1;
    JButton b1;
    JLabel l1, l2,l3,l4;
    JTextField t1,t2;
    JPasswordField s1;
    BookRecordService bookRecordService = new BookRecordServiceImpl();
    public UserLendtimeInterface(Long bookId, String account, Long maxLendTIme, Long num)
    {
        f1 = new JFrame("借阅信息选项");
        p1 = new JPanel();
        b1 = new JButton("确认借阅");
       // l1 = new JLabel("借阅时长：");
        l2 = new JLabel("借阅数量：");
       // l3 = new JLabel("（最长可借时间："+time+"天）");
        l4 = new JLabel("（剩余可借书本量："+num+"本）");
        //t1 = new JTextField(18);
        t2 = new JTextField(18);

        //限制输入借阅时长和数量只能是数字
        t2.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                char keych = e.getKeyChar();
                if((keych<'0')||(keych > '9'))
                {
                    if(keych!=' ')
                        e.setKeyChar('\0');
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {

            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });

        p1.setLayout(null);
        b1.setBounds(200,220,100,30);
        //l1.setBounds(100,110,80,30);
        l2.setBounds(100,160,80,30);
        //l3.setBounds(360,110,500,30);
        l4.setBounds(360,160,500,30);
        //t1.setBounds(160,110,200,30);
        t2.setBounds(160,160,200,30);

        f1.add(p1);
       // p1.add(l1);
        p1.add(l2);
        //p1.add(l3);
        p1.add(l4);
        //p1.add(t1);
        p1.add(t2);
        p1.add(b1);

        BookService bookService =new BookServiceImpl();

        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<BookVo> books = bookService.listBookByBookId(bookId);
                BookVo book = books.get(0);
                Long surplusNumber = book.getSurplusNumber();
                //如果输入为空或者是空格，则弹出格式不正确
                if(t2.getText().equals("0") || t2.getText().equals(""))
                {
                    new MyDialogDemo("书籍数格式不正确！");
                    return ;
                }
                //如果剩余书本量不足
                if(surplusNumber< Long.valueOf(t2.getText()))
                {
                    new MyDialogDemo("库存不足");
                    return ;
                }
                //如果借书数量超过了上限
                if(num < Long.valueOf(t2.getText()))
                {
                    new MyDialogDemo("借书数量超过上限");
                    return ;
                }
                boolean flag = bookRecordService.LendBook(bookId, account,maxLendTIme, Long.valueOf(t2.getText()));
                if(flag){
                    f1.dispose();
                    new MyDialogDemo("借阅成功");
                }
            }});
        f1.setVisible(true);
        f1.setBounds(600,250,600,400);
        }

    }
